﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class SubjectBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IGeneralLedgerDetailDA generalLedgerDetailDA = DataAccess.CreateGeneralLedgerDetailDA();
        private PubParasInfo pubParasInfo;
        private static readonly ISubjectDA subjectDA = DataAccess.CreateSubjectDA();

        public SubjectBLL()
        {
            this.pubParasInfo = null;
        }

        public SubjectBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public SubjectBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> IDS)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string temp = string.Empty;
            try
            {
                int i;
                List<SubjectInfo> SubjectInfos = new List<SubjectInfo>();
                SubjectInfo subjectInfo = null;
                foreach (string id in IDS)
                {
                    subjectInfo = subjectDA.GetSubjectByID(dataBase, tran, id, this.dataBaseID[0]);
                    SubjectInfos.Add(subjectInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                string count = "";
                foreach (string id in IDS)
                {
                    DataSet ds = generalLedgerDetailDA.CheckGeneralLedgerDetailIDBySubjectID(dataBase, tran, id, this.dataBaseID[0]);
                    i = 0;
                    while (i < ds.Tables[0].Rows.Count)
                    {
                        count = count + ds.Tables[0].Rows[i][0] + ",";
                        i++;
                    }
                    if (!string.IsNullOrEmpty(count))
                    {
                        count = count.Substring(0, count.Length - 1);
                        throw new Exception("科目:" + id + "在总账分配" + count + "中有引用,无法删除!请先删除总账分配再删除此科目!");
                    }
                    ds.Clear();
                }
                if (subjectDA.DeleteSubject(dataBase, tran, IDS, this.dataBaseID[0]) > 0)
                {
                    for (i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<SubjectInfo>(dataBase, tran, SubjectInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "SUBJECT", this.dataBaseID[0]);
                        temp = temp + IDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public string DeleteSubject(List<string> IDS)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string temp = string.Empty;
            try
            {
                List<SubjectInfo> SubjectInfos = new List<SubjectInfo>();
                SubjectInfo subjectInfo = null;
                foreach (string id in IDS)
                {
                    subjectInfo = subjectDA.GetSubjectByID(dataBase, tran, id, this.dataBaseID[0]);
                    SubjectInfos.Add(subjectInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (subjectDA.DeleteSubject(dataBase, tran, IDS, this.dataBaseID[0]) > 0)
                {
                    for (int i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<SubjectInfo>(dataBase, tran, SubjectInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "SUBJECT", this.dataBaseID[0]);
                        temp = temp + IDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return subjectDA.GetAllSubject(this.dataBaseID[0]);
        }

        public object GetInfoByID(string id)
        {
            return subjectDA.GetSubjectByID(id, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            SubjectQueryEntity subjectQueryEntity = obj as SubjectQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(subjectQueryEntity.SubjectID))
            {
                sqlWhere.Add(" SUBJECT_ID " + subjectQueryEntity.SubjectID);
            }
            if (!string.IsNullOrEmpty(subjectQueryEntity.SubjectName))
            {
                sqlWhere.Add(" SUBJECT_NAME " + subjectQueryEntity.SubjectName);
            }
            if (!string.IsNullOrEmpty(subjectQueryEntity.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + subjectQueryEntity.IsActive);
            }
            return subjectDA.GetSubjectByQueryList(sqlWhere, subjectQueryEntity, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            SubjectInfo subjectInfo = obj as SubjectInfo;
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strContractID = string.Empty;
            try
            {
                if (string.IsNullOrEmpty(subjectInfo.SubjectID))
                {
                    subjectInfo.SubjectID = commonMethod.GenNextNumStr(dataBase, tran, "SUBJECT", whLoginID);
                }
                subjectInfo.UpdatedBy = subjectInfo.CreatedBy;
                subjectInfo.CreatedDate = DateTime.Now;
                subjectInfo.UpdatedDate = DateTime.Now;
                if (subjectDA.InsertSubject(dataBase, tran, subjectInfo, whLoginID) > 0)
                {
                    strContractID = subjectInfo.SubjectID;
                    new AuditDataBLL().InsertAuditData<object>(dataBase, tran, null, obj, AuditAction.Insert, this.pubParasInfo.UserID, "SUBJECT", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strContractID;
        }

        public string Update(object obj)
        {
            SubjectInfo subjectInfo = obj as SubjectInfo;
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strContractID = string.Empty;
            try
            {
                SubjectInfo oldSubjectInfo = subjectDA.GetSubjectByID(dataBase, tran, subjectInfo.SubjectID, this.dataBaseID[0]);
                if (subjectDA.UpdateSubject(dataBase, tran, subjectInfo, whLoginID) > 0)
                {
                    strContractID = subjectInfo.SubjectID;
                    new AuditDataBLL().InsertAuditData<object>(dataBase, tran, oldSubjectInfo, obj, AuditAction.Update, this.pubParasInfo.UserID, "SUBJECT", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strContractID;
        }
    }
}

